package com.school.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.school.model.Admin;
import com.school.model.Comment;
import com.school.model.Cuisine;
import com.school.model.District;
import com.school.model.Eatery;
import com.school.model.Order;
import com.school.model.User;
import com.school.service.impl.AdminServiceImpl;

public class AdminAction
{

    // ��ȡҳ�����
    private String loginId;
    private String loginPwd;

    AdminServiceImpl adminService = new AdminServiceImpl();

    // ����Ա��¼
    public String login()
    {
        Admin admin = adminService.adminLogin(loginId, loginPwd);
        if (admin != null)
        {
            return "index";
        }
        return "error";
    }

    // �鿴�û�
    public String displayUsers()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String state = request.getParameter("uState");
        int uState = Integer.parseInt(state);
        List<User> users = adminService.displayUsers(uState);
        if (users == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Users", users);
            if (uState == 1)
            {
                return "EmployeeMgr";
            }
            else
            {
                return "lockedUsers";
            }
        }
    }

    // �鿴�û���ϸ��Ϣ
    public String userDetails()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("userId");
        int userId = Integer.parseInt(id);
        User user = adminService.userDetails(userId);
        if (user == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("User", user);
            return "userDetails";
        }

    }

    // �����͹�
    public String addEatery()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String eLoginId = request.getParameter("eLoginId");
        String eateryPass = request.getParameter("eateryPass");
        String eateryPass1 = request.getParameter("eateryPass1");

        // �ж��������������Ƿ�һ��
        if (eateryPass.equals(eateryPass1))
        {
            int result = adminService.addEatery(eLoginId, eateryPass);
            if (result < 0)
            {
                return "error";
            }
            else
            {
                return "success";
            }
        }
        else
        {
            request.setAttribute("msg2", "�����������벻һ�£����������룡");
            return "addEatery";
        }
    }

    // �����������û�
    public String lockUser()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String state = request.getParameter("uState");
        int uState = Integer.parseInt(state);
        String id = request.getParameter("userId");
        int userId = Integer.parseInt(id);

        int result = adminService.lockUser(uState, userId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }

    // �鿴�͹�
    public String displayEaterys()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String state = request.getParameter("estate");
        int estate = Integer.parseInt(state);
        List<Eatery> eaterys = adminService.displayEaterys(estate);
        if (eaterys == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Eaterys", eaterys);
            if (estate == 1)
            {
                return "unlockedEatery";
            }
            else
            {
                return "lockedEatery";
            }
        }
    }

    // �鿴�͹���ϸ��Ϣ
    public String eateryDetails()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("eateryId");
        int eateryId = Integer.parseInt(id);
        Eatery eatery = adminService.eateryDetails(eateryId);
        if (eatery == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Eatery", eatery);
            return "eateryDetails";
        }

    }

    // �����������û�
    public String lockEatery()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String state = request.getParameter("estate");
        int estate = Integer.parseInt(state);
        String id = request.getParameter("eateryId");
        int eateryId = Integer.parseInt(id);

        int result = adminService.lockEatery(estate, eateryId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }

    // �鿴����
    public String displayOrders()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String state = request.getParameter("oState");
        int oState = Integer.parseInt(state);
        List<Order> orders = adminService.displayOrders(oState);
        if (orders == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Orders", orders);
            if (oState == 1)
            {
                return "processedOrders";
            }
            else
            {
                return "unprocessedOrders";
            }
        }
    }

    // ȡ������
    public String cancelOrder()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String orderId = request.getParameter("orderId");
        int result = adminService.cancelOrder(orderId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }
    
    //�鿴����
    public String displayComments()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<Comment> comments = adminService.displayComments();
        if (comments == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Comments", comments);
            return "displayComments";
        }
    }
    
    //ɾ������
    public String deleteComment()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("commentId");
        int commentId = Integer.parseInt(id);
        int result = adminService.deleteComment(commentId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }
    
    //�鿴����Ա
    public String displayAdmins()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String state = request.getParameter("aState");
        int aState = Integer.parseInt(state);
        List<Admin> admins = adminService.displayAdmins(aState);
        if (admins == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Admins", admins);
            if (aState == 1)
            {
                return "unlockedAdmins";
            }
            else
            {
                return "lockedAdmins";
            }
        }
    }
    
    //��������������Ա
    public String lockAdmin()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String state = request.getParameter("aState");
        int aState = Integer.parseInt(state);
        String id = request.getParameter("adminId");
        int adminId = Integer.parseInt(id);

        int result = adminService.lockAdmin(aState, adminId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }
    
    //ɾ������Ա
    public String deleteAdmin()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("adminId");
        int adminId = Integer.parseInt(id);
        int result = adminService.deleteAdmin(adminId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }
    
    //���ӹ���Ա
    public String addAdmin()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String adminName = request.getParameter("adminName");
        String adminPass = request.getParameter("adminPwd");
        String adminPass1 = request.getParameter("adminPwd1");

        // �ж��������������Ƿ�һ��
        if (adminPass.equals(adminPass1))
        {
            int result = adminService.addAdmin(adminName, adminPass);
            if (result < 0)
            {
                return "error";
            }
            else
            {
                return "success";
            }
        }
        else
        {
            request.setAttribute("msg1", "�����������벻һ�£����������룡");
            return "addAdmin";
        }
    }
    
    //�鿴����͹�
    public String displayDistrictEatery()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<District> districts = adminService.displayDistrictEatery();
        
        if (districts == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Districts", districts);
            return "displayDistrict";
        }
    }
    
    //�޸���������
    public String alterDistrict()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("districtId");
        int districtId = Integer.parseInt(id);
        String districtName = request.getParameter("districtName");
        
        int result = adminService.alterDistrict(districtName, districtId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }
    
    //�鿴��������
    public String displayDistrictName()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("districtId");
        int districtId = Integer.parseInt(id);
        District district = adminService.displayDistrictName(districtId);
        if (district == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("district", district);
            return "updateDistrict";
        }
    }
    
    //ɾ������
    public String deleteDistrict()
    {
        
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("districtId");
        String num = request.getParameter("districtNum");
        int districtId=Integer.parseInt(id);
        int districtNum=Integer.parseInt(num);
        if(districtNum==0){
            int result=adminService.deleteDistrict(districtId);
            if(result<0){
                return "error";
            }
            else{
                return "success";
            }
        }
        else{
            request.setAttribute("msg3", "ɾ��ʧ�ܣ�����������ע��Ĳ͹�");
            return "displayDistrict";
        }
        
    }
    
    //�������
    public String addDistrict()
    {
            HttpServletRequest request = ServletActionContext.getRequest();
            String districtName = request.getParameter("districtName");

            int result = adminService.addDistrict(districtName);
            if (result < 0)
            {
                return "error";
            }
            else
            {
                return "success";
            }
    }
    
  //�鿴��ϵ��Ʒ
    public String displayCuisineFood()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        List<Cuisine> cuisines = adminService.displayCuisineFood();
        
        if (cuisines == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("Cuisines", cuisines);
            return "displayCuisine";
        }
    }
   
  //�鿴��ϵ����
    public String displayCuisineName()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("cuisineId");
        int cuisineId = Integer.parseInt(id);
        Cuisine cuisine = adminService.displayCuisineName(cuisineId);
        if (cuisine == null)
        {
            return "error";
        }
        else
        {
            request.setAttribute("cuisine", cuisine);
            return "updateCuisine";
        }
    }
    
  //�޸Ĳ�ϵ����
    public String alterCuisine()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("cuisineId");
        int cuisineId = Integer.parseInt(id);
        String cuisineName = request.getParameter("cuisineName");
        
        int result = adminService.alterCuisine(cuisineName, cuisineId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }
    
    //ɾ������
    public String deleteCuisine()
    {
        
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("cuisineId");
        String num = request.getParameter("cuisineNum");
        int cuisineId=Integer.parseInt(id);
        int cuisineNum=Integer.parseInt(num);
        if(cuisineNum==0){
            int result=adminService.deleteCuisine(cuisineId);
            if(result<0){
                return "error";
            }
            else{
                return "success";
            }
        }
        else{
            request.setAttribute("msg4", "ɾ��ʧ�ܣ��ò�ϵ���в�Ʒ");
            return "displayCuisine";
        }
        
    }
    
  //��Ӳ�ϵ
    public String addCuisine()
    {
            HttpServletRequest request = ServletActionContext.getRequest();
            String cuisineName = request.getParameter("cuisineName");

            int result = adminService.addCuisine(cuisineName);
            if (result < 0)
            {
                return "error";
            }
            else
            {
                return "success";
            }
    }
    
    
    

    public String getLoginId()
    {
        return loginId;
    }

    public void setLoginId(String loginId)
    {
        this.loginId = loginId;
    }

    public String getLoginPwd()
    {
        return loginPwd;
    }

    public void setLoginPwd(String loginPwd)
    {
        this.loginPwd = loginPwd;
    }

}
